/**
 * @author Hemant
 * Tag.java
 * This class handles all the HTML tags. Set properties and attributes for
 * all the tags. 
 */

/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  guiproperty;

import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Vector;

import dbmanager.DBManager;
import dbmanager.GetIdName;
import utility.CapitalChar;

/**
 * This class handles all tag type tag objects and generates HTML code for them.
 *
 */
public class Tag implements HTMLInterface
{
	private String header,ObjectName,param = "",line="",tag="",report="",text="",select,noselect;
	private LinkedList<java.lang.Object> PropertyList;
	Hashtable<Object, Object> hash;
	private Hashtable<Object, LinkedList<Object>> ObjProp;
	GetIdName get;
	Vector<Object> SingleTags = new Vector<Object>();
	
	/**
	 * This method sets object name for tag type object. 
	 */
	public void setObjectName(String ObjectName) 
	{
		this.ObjectName = ObjectName;
		SingleTags.add("br");
		SingleTags.add("input");
		SingleTags.add("hr");
		/*SingleTags.add("label");
		SingleTags.add("br");
		SingleTags.add("br");
		SingleTags.add("br");
		SingleTags.add("br");*/
	}
	
	public void setReport(String report) 
	{
		this.report = report;
	}
	public void setText(String text) 
	{
		this.text = text;
	}
	
	public void setGetIdName(GetIdName get) 
	{
		this.get = get;
	}
	
	public void setTag(String tag) 
	{
		this.tag = tag;
	}
	public void setLine(String line) 
	{
		this.line = line;
	}
    public void setParam(String param) 
	{
		this.param = param;		
	}
    
	/**
	 * This method sets property list for specified object name.
	 */
	public void setObjPropertyList(Hashtable<java.lang.Object, LinkedList<java.lang.Object>> ObjectPropertyList) 
	{
		this.ObjProp = ObjectPropertyList;
		try
		{
			if(ObjectName != null || !ObjectName.trim().equalsIgnoreCase(""))
			{
				this.PropertyList = ObjectPropertyList.get(ObjectName);
			}
		}
		catch (NullPointerException e) {
			
		}		
	}
	
	/**
	 * This method generate HTML code for specified tag object.
	 * @return String containing HTML code.
	 */
	public String getHTMLCode()
	{
		try {
			int size = PropertyList.size();
			
			StringBuffer buffer = new StringBuffer();
			buffer.append("");
			
			StringBuffer propbuffer = new StringBuffer();
			propbuffer.append("");
			
			StringBuffer attBuffer = new StringBuffer();
			attBuffer.append("");
			
			Vector<Object> ClassVector = new Vector<Object>();
			
			for(int i = 0;i < size;i++ )
			{
				String [] property = PropertyList.get(i).toString().split(String.valueOf('\t'));
				System.out.println("The property arry is "+PropertyList.get(i));
				
				if(property[0].equalsIgnoreCase("type"))
				{
					tag = property[1];
				}				
				else if(property[0].equalsIgnoreCase("text"))
				{
					int start_loc = property[1].indexOf("$");
					int end_loc = property[1].lastIndexOf("$");
					
					String param = null;
					if(property[1].startsWith("$") && property[1].endsWith("$") && (start_loc != end_loc))
					{
						param = property[1].substring(1, property[1].length()-1);
						text = (String)hash.get(param.toLowerCase());
						
						try {
							if(text == null || text.equalsIgnoreCase("null"))
								text = property[1];
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
					else
						text = property[1];
				}
				else if(property[0].equalsIgnoreCase("select"))
				{
					select = property[1];
				}
				else if(property[0].equalsIgnoreCase("noselect"))
				{
					noselect = property[1];
				}
				else
				{
					String classname = CapitalChar.makeFirstCharCapital(property[0]);
					System.out.println("The classname is "+classname);
					Class<Object> myclass;
					
					try 
					{
						myclass = (Class<Object>)Class.forName("guiproperty."+classname);
						
						HTMLInterface obj;
						try 
						{
							obj = (HTMLInterface)myclass.newInstance();
													
							if(classname.equals("Attribute"))
							{
								obj.setGetIdName(get);
								obj.setText(text);
								obj.setObjectName(property[1]);
								obj.setObjPropertyList(ObjProp);
								obj.setTag(tag);
								obj.setParam(param);
								obj.setLine(line);
								obj.setSelectObject(select);
								obj.setNoSelectObject(noselect);
								obj.setReport(report);
								obj.setParamHash(hash);							
								attBuffer.append(obj.getHTMLCode());
							}
							else
							{
								ClassVector.add(obj);
								ClassVector.add(property[1]);
								
							}
						}
						catch (InstantiationException e) 
						{
							e.printStackTrace();
						} 
						catch (IllegalAccessException e) 
						{
							e.printStackTrace();
						}					
					}
					catch (ClassNotFoundException e) 
					{
						e.printStackTrace();
					}							
				}
			}
			
			int vector_size = ClassVector.size();
			
			if(vector_size == 0)
				propbuffer.append(text);
			
			for(int i=0;i<vector_size;i=i+2)
			{
				HTMLInterface obj = (HTMLInterface)ClassVector.get(i);
				obj.setGetIdName(get);
				
				if(!(text == null || text.equalsIgnoreCase("")))
				{
					if(text.startsWith("<"))
						obj.setText(text);
					else
						obj.setText(CapitalChar.makeFirstCharCapital(text));
				}
				
				obj.setObjectName((String)ClassVector.get(i+1));
				obj.setObjPropertyList(ObjProp);
				obj.setTag(tag);
				obj.setParam(param);
				obj.setLine(line);
				obj.setSelectObject(select);
				obj.setNoSelectObject(noselect);
				obj.setReport(report);
				obj.setParamHash(hash);		
				propbuffer.append(obj.getHTMLCode());
			}
			
			if(SingleTags.contains(tag.toLowerCase()))
			{
				buffer.append("<"+tag+" "+attBuffer+">");
				buffer.append(propbuffer.toString());
			}
			else if(tag == null || tag.trim().equalsIgnoreCase(""))
			{
				buffer.append(attBuffer);
				buffer.append(propbuffer.toString());
			}
			else
			{
				buffer.append("<"+tag+" "+attBuffer+">");
				buffer.append(propbuffer.toString());
				buffer.append("</"+tag+">");
			}
			
			System.out.println("The string buffer in Tag class is "+buffer.toString());
			return buffer.toString();
		} catch (Exception e) {
			return "";
		}				
	}

	public void setParamHash(Hashtable<Object, Object> hash) {
		// TODO Auto-generated method stub
		this.hash = hash;
	}

	public void setNoSelectObject(String noselect) {
		// TODO Auto-generated method stub
		
	}

	public void setSelectObject(String select) {
		// TODO Auto-generated method stub
		
	}

	public void isEnd(boolean end) {
		// TODO Auto-generated method stub
		
	}

	public void setHeaderLine(String header) {
		// TODO Auto-generated method stub
		this.header = header;
	}

	
}
